package com.prs.crm.dao.crm.impl;

import java.sql.SQLException;
import java.util.Collection;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.crm.CustomerAppointDao;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.domain.crm.Customer;
import com.prs.crm.domain.crm.CustomerAppoint;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.service.crm.CustomerService;
import com.prs.crm.service.crm.LinkmanService;

public class CustomerAppointDaoImpl extends BaseDaoImpl<CustomerAppoint> implements CustomerAppointDao{
	private LinkmanService linkmanService;
	private CustomerService customerService;
	
	@Override
	public void removeByCustomer(Customer customer) {
		// TODO Auto-generated method stub
		Query query = this.getSession().createQuery("delete from CustomerAppoint where customer=?");
		query.setParameter(0, customer);
		query.executeUpdate();
	}
	
	@SuppressWarnings("unchecked")
	public Collection<Integer> findPersonIds()
	{
		 return (Collection<Integer>)this.getHibernateTemplate().execute(
					new HibernateCallback()
					{
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(CustomerAppoint.class);
				        	criteria.createAlias("person", "p");
				        	criteria.setProjection(Projections.distinct(Projections.property("p.id")));
				        	return criteria.list();
				        }   
					}
			);
	}


	public void setLinkmanService(LinkmanService linkmanService) {
		this.linkmanService = linkmanService;
	}

	public LinkmanService getLinkmanService() {
		return linkmanService;
	}

	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}

	public CustomerService getCustomerService() {
		return customerService;
	}

	@Override
	@SuppressWarnings("unchecked")
	public Collection<CustomerAppoint> getCustomerAppoint(final Person person) {
		// TODO Auto-generated method stub
		return (Collection<CustomerAppoint>) this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(CustomerAppoint.class);
				criteria.add(Restrictions.eq("person", person));
				return criteria.list();
			}
			
		});
	}

	@Override
	@SuppressWarnings("unchecked")
	public Collection<Object[]> listPersonAppoint() {
		// TODO Auto-generated method stub
		return (Collection<Object[]>) this.getHibernateTemplate().execute(new HibernateCallback(){

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(CustomerAppoint.class);
				ProjectionList projectionlist=Projections.projectionList();  
		    	projectionlist.add(Projections.groupProperty("person"));
		    	projectionlist.add(Projections.count("customer"));
		    	criteria.setProjection(projectionlist);
				return criteria.list();
			}
			
		});
	}

}
